﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.DataVisualization.Charting;
using System.Windows.Forms;


namespace DesktopApplication
{
    public partial class LoginSuccess : Form
    {
        public LoginSuccess()
        {
            InitializeComponent();
        }

        private void LoginSuccess_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“blogsDataSet1.Articles”中。您可以根据需要移动或删除它。
            this.articlesTableAdapter.Fill(this.blogsDataSet1.Articles);
            // TODO: 这行代码将数据加载到表“blogsDataSet.Users”中。您可以根据需要移动或删除它。
            this.usersTableAdapter1.Fill(this.blogsDataSet.Users);
            // TODO: 这行代码将数据加载到表“admin001DataSet.Users”中。您可以根据需要移动或删除它。
            this.usersTableAdapter.Fill(this.admin001DataSet.Users);

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string cmd = "select * from Articles";

            var dt = DBhelper.GetDataTable(cmd);

            dataGridView1.DataSource = dt;

            dataGridView1.ReadOnly = true; //只读

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //单机标头或该行所的单元格，选择整行

            dataGridView1.AllowUserToAddRows = false;  //删除新行
        }

        //增加
        private void button1_Click(object sender, EventArgs e)
        {


            AddForm addForm = new AddForm();
            addForm.Show();

        }

        //删除
        private void button2_Click(object sender, EventArgs e)
        {
            int rowindex = dataGridView1.CurrentRow.Index;
            string value0 = dataGridView1.Rows[rowindex].Cells[0].Value.ToString();
            string sql = "delete from Articles where id = '" + value0 + "'";
            DBhelper.AddOrUpdateOrDelete(sql);
            MessageBox.Show("删除成功！");


        }

        //修改
        private void button3_Click(object sender, EventArgs e)
        {


            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var Title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var Author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                EditForm editForm = new EditForm(id, Title, Author);
                var res = editForm.ShowDialog();
                if (res == DialogResult.Yes)
                {
                    var name = Text.Length;

                    var sql = string.Format("select * from Articles where Author like '%{0}%'", name);
                    MessageBox.Show("修改成功！");

                    var dataTable = DBhelper.GetDataTable(sql);

                    dataGridView1.DataSource = dataTable;
                }
                else
                {
                    MessageBox.Show("修改失败失败！");
                }
            }
            else
            {
                MessageBox.Show("先选择行");
            }


        }
        //查询
        private void button4_Click(object sender, EventArgs e)
            {
                var name = Find.Text;

                var sql = string.Format("select * from Articles where Author like '%{0}%'", name);

                var dt = DBhelper.GetDataTable(sql);

                dataGridView1.DataSource = dt;
            }

            private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
            {

            }
        }
    } 

